package com.jsnkj.basic.dialog

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.jsnkj.basic.R
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.util.SharePreferencesUtil
import com.jsnkj.basic.util.DoubleClickHelper1
//import com.tb.mob.bean.Position


/**
 * 打开红包弹窗
 */
class OpenMainRedDialog(
    var dialogContext: Activity,
    var click: onItemClickListener
) :
    Dialog(dialogContext), View.OnClickListener {

    var tvMoney: TextView? = null
    var frameLayoutAd: FrameLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_open_red_main)
        setCanceledOnTouchOutside(false)
        setCancelable(true)
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        val imgClose: ImageView = findViewById(R.id.img_close)
        imgClose.setOnClickListener(this)
        imgClose.visibility = View.GONE

        tvMoney = findViewById(R.id.tv_money)

//        findViewById<TextView>(R.id.tv_immediate_claim).setOnClickListener(this)
//        findViewById<TextView>(R.id.tv_immediate_claim1).setOnClickListener(this)

        DoubleClickHelper1.click(findViewById<TextView>(R.id.tv_immediate_claim)) {
            dismiss()
            click.openItemClickListener()
        }

        DoubleClickHelper1.click(findViewById<TextView>(R.id.tv_immediate_claim1)) {
            dismiss()
            click.openItemClickListener()
        }


        DoubleClickHelper1.click(findViewById<ImageView>(R.id.img_increase_reward)) {
            dismiss()
            click.increaseRewardCallBack()
        }


        frameLayoutAd = findViewById(R.id.fl_ad)

        //加载信息流
//        AdvertisementManager.loadFeed(
//            dialogContext, 300, 169,
//            object : com.tb.mob.TbManager.FeedLoadListener {
//                override fun onFail(p0: String?) {
//                    BurialPointUtil.flowAdEvent("fail")
//                }
//
//                override fun onLoad(feedPosition: FeedPosition) {
//                    BurialPointUtil.flowAdEvent("show")
//                    feedPosition.showFeed(dialogContext, frameLayoutAd)
//                }
//
//                override fun onDismiss() {
//
//                }
//
//                override fun onClicked() {
//
//                }
//
//                override fun onExposure(position: Position?) {
//                    position?.let {
//                        RewardsUtil.addGoldNumber(it.ecpm, RewardsConfig.adStream)
//                    }
//                }
//
//                override fun onVideoReady() {
//
//                }
//
//                override fun onVideoComplete() {
//
//                }
//
//            })

        Handler().postDelayed({
            imgClose.visibility = View.VISIBLE
        }, 300)
    }


    fun showDialog() {
        this.show()

        if (null == tvMoney) {
            tvMoney = findViewById(R.id.tv_money)

        }
        val tvImmediateClaim: TextView? = findViewById(R.id.tv_immediate_claim)
        val tvTips: TextView? = findViewById(R.id.tv_tips)


        val conLayout: ConstraintLayout? = findViewById(R.id.con_layout1)

        val maxRedpackAmount =
            SharePreferencesUtil.getInstance().getSettingEntity().max_redpack_amount

        if (RewardsConfig.goldMoney > maxRedpackAmount) {
            RewardsConfig.goldMoney = maxRedpackAmount
        }

        if (maxRedpackAmount > 200 && RewardsConfig.goldMoney < 2000) {
            conLayout?.visibility = View.VISIBLE
            tvImmediateClaim?.visibility = View.GONE
            tvTips?.visibility = View.GONE
        } else {
            conLayout?.visibility = View.GONE
            tvImmediateClaim?.visibility = View.VISIBLE
            tvTips?.visibility = View.VISIBLE
        }

        com.jsnkj.basic.util.LogUtils.e("当前金币数量：${RewardsConfig.goldMoney}   maxRedpackAmount == $maxRedpackAmount")
        tvMoney?.text = RewardsConfig.goldMoney.toString()
    }


    override fun onClick(view: View) {
        dismiss()
        when (view.id) {
            R.id.img_close -> {
                click.closeCallBack()
            }

//            R.id.img_increase_reward -> { //提高奖励
//                click.increaseRewardCallBack()
//            }
//
//            R.id.tv_immediate_claim, R.id.tv_immediate_claim1 -> {
//                click.openItemClickListener()
//            }
        }
    }


    interface onItemClickListener {
        fun openItemClickListener()
        fun closeCallBack()
        fun increaseRewardCallBack()
    }

}